import { InjectionKey } from 'vue';
import {
  createStore, Store, StoreOptions, useStore as baseUseStore,
} from 'vuex';
import modules from '@/store/modules';
import getters from '@/store/getters';
import { StoreState } from '@/types';

const modulesObj = { ...modules };
for (const [key, val] of Object.entries(modulesObj)) {
  modulesObj[
    (key.charAt(0).toUpperCase() + key.slice(1)) as keyof typeof modulesObj
  ] = val;
  Reflect.deleteProperty(modulesObj, key);
}
export const key: InjectionKey<Store<StoreState>> = Symbol() as InjectionKey<Store<StoreState>>;
export const store = createStore<StoreState>({
  modules: {
    ...modulesObj,
  },
  getters,
} as StoreOptions<StoreState>);

export function useStore() {
  return baseUseStore(key);
}
